package com.hxzy.controller;

import com.hxzy.common.domain.AjaxResult;
import com.hxzy.common.page.TableDataInfo;
import com.hxzy.dto.RoleSearchDTO;
import com.hxzy.entity.AdminUser;
import com.hxzy.entity.Role;
import com.hxzy.framework.controller.BaseController;
import com.hxzy.service.RoleService;
import com.hxzy.vo.ZTree;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 类说明
 *
 * @author admin
 * @date 2021-09-07
 */
@Controller
@RequestMapping(value = "/admin")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    /**
     * 显示角色页面
     * @param model
     * @return
     */
    @GetMapping(value = "/role/list")
    public String showPage(Model model){
        return "system/role/list";
    }

    /**
     * 返回给前端bootstrap-table的数据
     * @return
     */
    @RequiresPermissions(value = "system:role:list")
    @ResponseBody
    @PostMapping(value = "/role/list")
    public TableDataInfo ajaxData(RoleSearchDTO roleSearchDTO, HttpServletRequest request){
        //分页参数以及分页操作 pagehelper
        super.startPage(request);
       //编写方法，查询
        List<Role> list = this.roleService.search(roleSearchDTO);


        return super.getDataTable(list);

    }

    /**
     * 根据角色id查询菜单ztree
     * @param roleId
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/role/{roleId}/owner/menu")
    public List<ZTree> roleMenuTreeData(@PathVariable(value = "roleId") Integer roleId){
        List<ZTree> treeList = this.roleService.roleMenuTreeData(roleId);
        return  treeList;
    }

    @RequiresPermissions(value = "system:role:add")
    @GetMapping(value = "/role/add")
    public  String addPage(Model model){
        return "system/role/add";
    }

    @RequiresPermissions(value = "system:role:add")
    @ResponseBody
    @PostMapping(value = "/role/add")
    public AjaxResult addData(Role  role, @RequestParam(value = "permsList")  List<Long> permsList){
        AdminUser  adminUser= (AdminUser) SecurityUtils.getSubject().getPrincipal();
        role.setCreateBy(adminUser.getLoginUserName());
        role.setUpdateBy(adminUser.getLoginUserName());
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());

        int count = this.roleService.addAndGrantMenu(role, permsList);
        return super.toAjax(count);
    }


    /**
     * 显示修改页面
     * @param roleId
     * @param model
     * @return
     */
    @RequiresPermissions(value = "system:role:edit")
    @GetMapping(value = "/role/edit/{roleId}")
    public  String editPage(@PathVariable(value = "roleId") Long roleId, Model model){
        Role role = this.roleService.selectByPrimaryKey(roleId);
        if(role==null){
            return "error/404";
        }

        model.addAttribute("role",role);
        return "system/role/edit";
    }

    @RequiresPermissions(value = "system:role:edit")
    @ResponseBody
    @PostMapping(value = "/role/edit")
    public AjaxResult editData(Role  role, @RequestParam(value = "permsList")  List<Long> permsList){
        AdminUser  adminUser= (AdminUser) SecurityUtils.getSubject().getPrincipal();

        role.setUpdateBy(adminUser.getLoginUserName());
        role.setUpdateTime(new Date());
        int count = this.roleService.editAndGrantMenu(role, permsList);
        return super.toAjax(count);
    }


    /**
     * 检查角色名称是否唯一
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/role/name/exists")
    public int existsRoleName(Integer id, String name){
        return this.roleService.existsRoleName(id,name);
    }

    /**
     * 检查角色标识是否唯一
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/role/perms/exists")
    public int existsRolePerms(Integer id, String perms){
        return this.roleService.existsRolePerms(id,perms);
    }


    @RequiresPermissions(value = "system:role:remove")
    @ResponseBody
    @PostMapping(value = "/role/remove")
    public AjaxResult remove( String ids){
        String[] arr=ids.split(",");
        int count=0;
        for(String  s : arr) {
           count+= this.roleService.deleteByPrimaryKey(Long.parseLong(s));
        }
        return super.toAjax(count);
    }

}
